package com.sdhs.paas.sdk.core.web.client;

import static com.sdhs.paas.sdk.core.web.PaasHttpHeader.RepDockerIp;
import static com.sdhs.paas.sdk.core.web.PaasHttpHeader.RepServUnitName;
import static com.sdhs.paas.sdk.core.web.PaasHttpHeader.RepServUnitVersion;
import static com.sdhs.paas.sdk.core.web.PaasHttpHeader.RepTimestamp;

import java.io.IOException;
import java.util.List;

import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.AsyncClientHttpRequestExecution;
import org.springframework.http.client.AsyncClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.support.HttpRequestWrapper;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

import com.sdhs.paas.sdk.core.web.PaasHttpHeader;

/**
 * HTTP请求拦截器，拦截外发请求，增加SDK 请求http header
 *
 * @author wjj
 *
 */
public class PaasHttpRequestInterceptor implements ClientHttpRequestInterceptor, AsyncClientHttpRequestInterceptor {

	@Autowired
	PaasHttpHeader paasHttpHeader;

	@Override
	public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
			throws IOException {
		HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);

		HttpHeaders httpHeaders = requestWrapper.getHeaders();

		paasHttpHeader.setPaasReqHeader(httpHeaders, request);

		ClientHttpResponse clientHttpResponse = execution.execute(requestWrapper, body);
		HttpHeaders repheader = clientHttpResponse.getHeaders();
		buildRepLog(request, repheader);

		return clientHttpResponse;
	}

	private void buildRepLog(HttpRequest request, HttpHeaders repheader) {

		String repTimestamp = getRepHeader(repheader, RepTimestamp);
		String repDockerIp = getRepHeader(repheader, RepDockerIp);
		String repServUnitName = getRepHeader(repheader, RepServUnitName);
		String repServUnitVersion = getRepHeader(repheader, RepServUnitVersion);
		MDC.put(RepTimestamp, repTimestamp);
		MDC.put(RepDockerIp, repDockerIp);
		MDC.put(RepServUnitName, repServUnitName);
		MDC.put(RepServUnitVersion, repServUnitVersion);
//		PaasLogger.metricInfo(
//				"本次请求url:{},响应头:RepTimestamp={}, RepDockerIp={}, RepServUnitName={},RepServUnitVersion={}",
//				request.getURI().toString(), repTimestamp, repDockerIp, repServUnitName, repServUnitVersion);
	}

	private String getRepHeader(HttpHeaders repheader, String headname) {
		String headVal = "";
		List<String> headers = repheader.get(headname);
		if (headers != null && headers.size() > 0) {
			headVal = headers.get(0);
		}
		return headVal;
	}

	@Override
	public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body,
			AsyncClientHttpRequestExecution execution) throws IOException {
		HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);

		HttpHeaders httpHeaders = requestWrapper.getHeaders();

		paasHttpHeader.setPaasReqHeader(httpHeaders, request);

		ListenableFuture<ClientHttpResponse> clientHttpResponse = execution.executeAsync(requestWrapper, body);

		clientHttpResponse.addCallback(new ListenableFutureCallback<ClientHttpResponse>() {

			@Override
			public void onSuccess(ClientHttpResponse result) {
				HttpHeaders repheader = result.getHeaders();
				buildRepLog(request, repheader);

			}

			@Override
			public void onFailure(Throwable ex) {
//				PaasLogger.metricError(ex, "本次请求url:{},异步请求失败！", request.getURI().toString());

			}

		});

		return clientHttpResponse;
	}

}
